package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqVar;
import cloud.maque.admin.mapper.MqVarMapper;
import cloud.maque.admin.service.MqVarService;
import cloud.maque.framework.common.exception.BusinessException;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author 4color
 * @description mq_var
 * @date 2023-03-17
 */
@Service
public class MqVarServiceImpl extends ServiceImpl<MqVarMapper, MqVar> implements MqVarService {

    @Override
    public MqVar saveVar(MqVar mqVar) {
        Assert.hasText(mqVar.getVarName(), "变量名称不能为空");
        Assert.hasText(mqVar.getGroupId(), "所在组不能为空");

        if (!StrUtil.isEmptyIfStr(mqVar.getVarId())) {
            MqVar oldVar = getByKey(mqVar.getTenantId(), mqVar.getVarKey().trim());
            if (!oldVar.getVarId().equals(mqVar.getVarId())) {
                throw new BusinessException("已存在相同的键名：" + mqVar.getVarKey());
            }
        }

        saveOrUpdate(mqVar);
        return mqVar;
    }

    @Override
    public Integer countByGroupId(String groupId) {
        QueryWrapper<MqVar> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqVar::getGroupId, groupId);
        return count(queryWrapper);
    }

    @Override
    public MqVar getByKey(String tenantId, String mqVar) {
        QueryWrapper<MqVar> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqVar::getTenantId, tenantId);
        queryWrapper.lambda().eq(MqVar::getVarKey, mqVar);
        return getOne(queryWrapper);
    }

    @Override
    public PageInfo<MqVar> page(PaginateDataResquest<MqVar> pageReq) {

        PageHelper.startPage(pageReq.getPageIndex(), pageReq.getPageSize());
        QueryWrapper<MqVar> wrapper = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getVarName())) {
            wrapper.lambda().like(MqVar::getVarName, pageReq.getData().getVarName());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getVarKey())) {
            wrapper.lambda().like(MqVar::getVarKey, pageReq.getData().getVarKey());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getGroupId()) && !pageReq.getData().getGroupId().equals("0")) {
            wrapper.lambda().eq(MqVar::getGroupId, pageReq.getData().getGroupId());
        }


        if (!StrUtil.isEmptyIfStr(pageReq.getData().getTenantId())) {
            wrapper.lambda().eq(MqVar::getTenantId, pageReq.getData().getTenantId());
        }

        wrapper.lambda().orderByAsc(MqVar::getVarOrder);
        wrapper.lambda().orderByAsc(MqVar::getVarId);
        List<MqVar> lists = this.baseMapper.selectList(wrapper);
        PageInfo<MqVar> pageInfo = new PageInfo<>(lists);

        return pageInfo;
    }

    @Override
    public Boolean delete(String varId, String tenantId) {

        QueryWrapper<MqVar> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MqVar::getTenantId, tenantId);
        wrapper.lambda().eq(MqVar::getVarId, varId);
        return remove(wrapper);
    }
}
